package com.qf.controller;/*
 *保护你的大熊
 *2021/12/20
 *17:07
 */

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.service.TestCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    RestTemplate template;

    @Autowired
    GoodsFeign goodsFeign;

    @Autowired
    TestCacheService testCacheService;


    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        testCacheService.clearCache(1);
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
    }


    @RequestMapping("/index")
    public String index(){
//        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("QF-GOODS", false);

        //获取对应模块的url
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url);

        //根据Eureka上注册的服务名称获取实例对象
//        ResponseEntity<String> responseEntity = template.getForEntity(url + "/index", String.class);

//        //远程调用商品模块的控制器
//        String url = "http://QF-GOODS/index";
        /**
         * getForEntity：模拟 http的get请求
         *  参数1：URL路径
         *  参数2：响应数据类型
         *  返回值：响应实体
         */
//        ResponseEntity<String> responseEntity = template.getForEntity(url, String.class);
//        String body = responseEntity.getBody();
//        System.out.println("响应体：" + body);
//        HttpStatus statusCode = responseEntity.getStatusCode();
//        System.out.println(statusCode.is2xxSuccessful() ? "成功" : "失败");

        String body = goodsFeign.index();

        return "获取订单信息" + "&" + body;
    }

    @HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
    })
    @GetMapping("/findById")
    public Goods findById(Integer id){
        return goodsFeign.findById2(id);
    }

    public Goods findByIdFallback(Integer id){
        return new Goods(id,"服务降级了");
    }

    @GetMapping("/save")
    public String saveGoods(Goods goods){
        return goodsFeign.save(goods);
    }

}
